﻿using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using QuickUIT.Controls;

namespace QuickUIT.Tests
{
    [TestClass]
    public class UISliderTests
    {
        private BaseState _baseState;

        [TestInitialize]
        public void TestSetup()
        {
            _baseState = new BaseState(TestUtilities.GetTestApplicationFileName(), "@Name='QuickUIT Test Application'");
            _baseState.Execute();
        }

        [TestMethod]
        public void ShouldGetMinimumValue()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UISlider slider = window.UISlider("@AutomationId='slider1'");
            Assert.AreEqual(0.0, slider.Minimum);
        }

        [TestMethod]
        public void ShouldGetMaximumValue()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UISlider slider = window.UISlider("@AutomationId='slider1'");
            Assert.AreEqual(10.0, slider.Maximum);
        }

        [TestMethod]
        public void ShouldGetCurrentValue()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UISlider slider = window.UISlider("@AutomationId='slider1'");
            Assert.AreEqual(0.0, slider.RangeValue);
        }

        [TestMethod]
        public void ShouldGetSmallChangeValue()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UISlider slider = window.UISlider("@AutomationId='slider1'");
            Assert.AreEqual(0.1, slider.SmallChange);
        }

        [TestMethod]
        public void ShouldGetLargeChangeValue()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UISlider slider = window.UISlider("@AutomationId='slider1'");
            Assert.AreEqual(1.0, slider.LargeChange);
        }

        [TestMethod]
        public void ShouldGetIsReadOnlyProperty()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UISlider slider = window.UISlider("@AutomationId='slider1'");
            Assert.IsFalse(slider.IsRangeReadOnly);
        }

        [TestMethod]
        public void ShouldSetValue()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UISlider slider = window.UISlider("@AutomationId='slider1'");
            Assert.AreEqual(0.0, slider.RangeValue);
            slider.SetRangeValue(5.0);
            Assert.AreEqual(5.0, slider.RangeValue);

            slider.RangeValue = 0.0; 
            Assert.AreEqual(0.0, slider.RangeValue);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentOutOfRangeException))]
        public void ShouldThrowExceptionWhenSettingValueBelowMinimum()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UISlider slider = window.UISlider("@AutomationId='slider1'");
            slider.SetRangeValue(-1.0);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentOutOfRangeException))]
        public void ShouldThrowExceptionWhenSettingValueAboveMaximum()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UISlider slider = window.UISlider("@AutomationId='slider1'");
            slider.SetRangeValue(11.0);
        }

        [TestMethod]
        public void ShouldIncrementSliderBySmallChange()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UISlider slider = window.UISlider("@AutomationId='slider1'");
            Assert.AreEqual(0.0, slider.RangeValue);
            slider.IncrementBySmallChange();
            Assert.AreEqual(0.1, slider.RangeValue);
        }

        [TestMethod]
        public void ShouldIncrementSliderByLargeChange()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UISlider slider = window.UISlider("@AutomationId='slider1'");
            Assert.AreEqual(0.0, slider.RangeValue);
            slider.IncrementByLargeChange();
            Assert.AreEqual(1.0, slider.RangeValue);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentOutOfRangeException))]
        public void ShouldThrowExceptionOnSmallIncrementWhenMaximumReached()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UISlider slider = window.UISlider("@AutomationId='slider1'");
            slider.SetRangeValue(10.0);
            slider.IncrementBySmallChange();
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentOutOfRangeException))]
        public void ShouldThrowExceptionOnLargeIncrementWhenMaximumReached()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UISlider slider = window.UISlider("@AutomationId='slider1'");
            slider.SetRangeValue(10.0);
            slider.IncrementByLargeChange();
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentOutOfRangeException))]
        public void ShouldThrowExceptionOnSmallDecrementingWhenMinimumReached()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UISlider slider = window.UISlider("@AutomationId='slider1'");
            slider.SetRangeValue(0.0);
            slider.DecrementBySmallChange();
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentOutOfRangeException))]
        public void ShouldThrowExceptionOnLargeDecrementingWhenMinimumReached()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UISlider slider = window.UISlider("@AutomationId='slider1'");
            slider.SetRangeValue(0.0);
            slider.DecrementByLargeChange();
        }

        [TestMethod]
        public void ShouldDecrementSliderBySmallChange()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UISlider slider = window.UISlider("@AutomationId='slider1'");
            slider.SetRangeValue(10.0);
            Assert.AreEqual(10.0, slider.RangeValue);
            slider.DecrementBySmallChange();
            Assert.AreEqual(9.9, slider.RangeValue);
        }

        [TestMethod]
        public void ShouldDecrementSliderByLargeChange()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UISlider slider = window.UISlider("@AutomationId='slider1'");
            slider.SetRangeValue(10.0);
            Assert.AreEqual(10.0, slider.RangeValue);
            slider.DecrementByLargeChange();
            Assert.AreEqual(9.0, slider.RangeValue);
        }

        [TestMethod]
        public void ShouldGetCanSelectMultiple()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UISlider slider = window.UISlider("@AutomationId='slider1'");
            Assert.IsFalse(slider.CanSelectMultiple);
        }

        [TestMethod]
        public void ShouldGetIsSelectionRequired()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UISlider slider = window.UISlider("@AutomationId='slider1'");
            Assert.IsFalse(slider.IsSelectionRequired);
        }

        [TestMethod]
        public void ShouldGetAllSliders()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            List<UISlider> sliders = window.UISliders();
            Assert.AreEqual(1, sliders.Count);
            Assert.AreEqual("slider1", sliders[0].AutomationId);
        }

        [TestCleanup]
        public void TestTeardown()
        {
            _baseState.Cleanup();
        }
    }
}